<template>
  <div class="app-container">
    <div class="filter-container">
      <el-form :inline="true" :model="queryParams" class="form-inline">
        <el-form-item label="用户ID">
          <el-input 
            v-model="queryParams.userId" 
            placeholder="请输入用户ID" 
            clearable 
            type="number"
          />
        </el-form-item>
        <el-form-item label="优惠券ID">
          <el-input 
            v-model="queryParams.couponId" 
            placeholder="请输入优惠券ID" 
            clearable 
            type="number"
          />
        </el-form-item>
        <el-form-item label="使用状态">
          <el-select v-model="queryParams.status" placeholder="请选择使用状态" clearable>
            <el-option label="未使用" value="0" />
            <el-option label="已使用" value="1" />
            <el-option label="已过期" value="2" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">查询</el-button>
          <el-button @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    
    <el-table
      v-loading="loading"
      :data="userCouponList"
      border
      style="width: 100%"
    >
      <el-table-column prop="userCouponId" label="记录ID" width="100" align="center" />
      <el-table-column prop="userId" label="用户ID" width="100" align="center" />
      <el-table-column prop="userName" label="用户名" width="120" />
      <el-table-column prop="couponId" label="优惠券ID" width="100" align="center" />
      <el-table-column prop="couponName" label="优惠券名称" min-width="150" />
      <el-table-column label="优惠券类型" width="120" align="center">
        <template #default="scope">
          <el-tag :type="getCouponTypeTagType(scope.row.couponType)">
            {{ getCouponTypeText(scope.row.couponType) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="优惠信息" width="150" align="center">
        <template #default="scope">
          <div v-if="scope.row.couponType === '0'">
            满{{ scope.row.minAmount }}减{{ scope.row.discountAmount }}
          </div>
          <div v-else-if="scope.row.couponType === '1'">
            {{ (scope.row.discountRate * 10).toFixed(1) }}折
            <br>
            <span class="text-xs text-gray-500">
              最高减{{ scope.row.maxDiscountAmount }}
            </span>
          </div>
          <div v-else>
            免费券
          </div>
        </template>
      </el-table-column>
      <el-table-column label="使用状态" width="100" align="center">
        <template #default="scope">
          <el-tag :type="getUserCouponStatusTagType(scope.row.status)">
            {{ getUserCouponStatusText(scope.row.status) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="receiveTime" label="领取时间" width="160" />
      <el-table-column label="使用时间" width="160">
        <template #default="scope">
          {{ scope.row.useTime || '-' }}
        </template>
      </el-table-column>
      <el-table-column prop="expireTime" label="过期时间" width="160" />
    </el-table>
    
    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination
        v-model:current-page="queryParams.pageNum"
        v-model:page-size="queryParams.pageSize"
        :page-sizes="[10, 20, 50, 100]"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { getUserCoupons } from '@/api/coupon'
import { handlePageResponse } from '@/utils/pagination'

// 用户优惠券列表
const userCouponList = ref([])
// 总数据量
const total = ref(0)
// 加载状态
const loading = ref(false)

// 查询参数
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  userId: '',
  couponId: '',
  status: ''
})

// 获取优惠券类型文本
const getCouponTypeText = (type) => {
  const typeMap = {
    '0': '满减券',
    '1': '折扣券',
    '2': '免费券'
  }
  return typeMap[type] || '未知类型'
}

// 获取优惠券类型标签类型
const getCouponTypeTagType = (type) => {
  const typeMap = {
    '0': 'primary',
    '1': 'success',
    '2': 'warning'
  }
  return typeMap[type] || ''
}

// 获取用户优惠券状态文本
const getUserCouponStatusText = (status) => {
  const statusMap = {
    '0': '未使用',
    '1': '已使用',
    '2': '已过期'
  }
  return statusMap[status] || '未知状态'
}

// 获取用户优惠券状态标签类型
const getUserCouponStatusTagType = (status) => {
  const statusMap = {
    '0': 'success',
    '1': 'info',
    '2': 'danger'
  }
  return statusMap[status] || ''
}

// 初始化数据
onMounted(async () => {
  await fetchData()
})

// 获取用户优惠券列表数据
const fetchData = async () => {
  loading.value = true
  try {
    const res = await getUserCoupons(queryParams)
    const { list, total: totalCount } = handlePageResponse(res)
    userCouponList.value = list
    total.value = totalCount
  } catch (error) {
    console.error('获取用户优惠券列表失败:', error)
    ElMessage.error('获取用户优惠券列表失败')
  } finally {
    loading.value = false
  }
}

// 处理查询
const handleQuery = () => {
  queryParams.pageNum = 1
  fetchData()
}

// 重置查询
const resetQuery = () => {
  queryParams.userId = ''
  queryParams.couponId = ''
  queryParams.status = ''
  handleQuery()
}

// 处理页码变化
const handleCurrentChange = (val) => {
  queryParams.pageNum = val
  fetchData()
}

// 处理每页条数变化
const handleSizeChange = (val) => {
  queryParams.pageSize = val
  queryParams.pageNum = 1
  fetchData()
}
</script>

<style scoped>
.app-container {
  padding: 20px;
}

.filter-container {
  padding-bottom: 20px;
}

.pagination-container {
  margin-top: 20px;
  text-align: right;
}

.text-xs {
  font-size: 12px;
}

.text-gray-500 {
  color: #6b7280;
}
</style>

